﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Net.Sockets;
using System.Net;
using System.IO;

namespace V5_WPClient {
    public partial class frmMain : Form {

        private bool isBorderCast = true;
        private bool isListen = true;
        private bool IsSending;

        private Thread ThreadSendBorderCast;
        private Thread threadWatch;

        private Socket listen, clientSocket;
        public frmMain() {
            InitializeComponent();
            this.Listen();
        }
        //启动服务
        private void Listen() {
            this.listen = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            IPHostEntry entry = Dns.Resolve(Dns.GetHostName());
            IPEndPoint localEP = new IPEndPoint(IPAddress.Any, 4530);
            this.lblIP.Text = "IP : " + entry.AddressList[0];
            try {
                this.listen.Bind(localEP);
                this.listen.Listen(10);
                this.threadWatch = new Thread(new ThreadStart(this.StartWatch));
                this.threadWatch.IsBackground = true;
                this.threadWatch.Start();
                this.ShowMsg("启动服务成功，等待手机连接......");
            }
            catch (Exception exception) {
                this.ShowMsg("启动服务失败,请重试。" + exception.ToString());
                this.listen.Close();
            }
        }
        //连接手机连接
        private void StartWatch() {
            while (this.isListen) {
                this.clientSocket = this.listen.Accept();
                this.ShowMsg("接收手机端连接成功!");
                Thread.Sleep(500);
            }
        }

        public void ShowMsg(string msgStr) {
            if (this.txtLogView.InvokeRequired) {
                this.txtLogView.Invoke(new MethodInvoker(() => {
                    this.txtLogView.Text = msgStr;
                }));
            }
            else {
                this.txtLogView.Text = msgStr;
            }

        }

        #region 操作按钮
        string FILE_PATH = string.Empty;
        //同步
        const int blockLength = 512 * 1024;
        private void btnSend_Click(object sender, EventArgs e) {
            if (clientSocket != null) {
                if (!clientSocket.Connected ) {
                    return;
                }
                //发送文件名称和大小
                clientSocket.Send(Encoding.UTF8.GetBytes("不灭之旅"));
                string filePath = FILE_PATH;
                int length;
                byte[] buffer = new Byte[blockLength];
                using (FileStream fs = File.Open(filePath, FileMode.Open)) {
                    long fileLength = fs.Length;
                    while (fileLength > 0) {
                        length = fs.Read(buffer, 0, blockLength);
                        clientSocket.Send(buffer, length, SocketFlags.None);
                        fileLength = fileLength - length;
                    }
                    clientSocket.Send(Encoding.UTF8.GetBytes("^"));
                    this.ShowMsg("数据发送完成!");
                }
            }
        }
        //备份
        private void btnBak_Click(object sender, EventArgs e) {

        }
        //导入
        private void btnExport_Click(object sender, EventArgs e) {

        }
        #endregion

        private void panelPC_DragDrop(object sender, DragEventArgs e) {

        }

        private void panelPC_DragEnter(object sender, DragEventArgs e) {

        }

        private void btnFileBorswer_Click(object sender, EventArgs e) {
            OpenFileDialog openFile = new OpenFileDialog();
            if (openFile.ShowDialog() == System.Windows.Forms.DialogResult.OK) {
                FILE_PATH = openFile.FileName;
            }

        }
    }
}
